package org.jeecg.modules.demo.sermanager.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.sermanager.entity.NetptDetectionRet;
import org.jeecg.modules.demo.sermanager.entity.NetptDetection;
import org.jeecg.modules.demo.sermanager.vo.NetptDetectionPage;
import org.jeecg.modules.demo.sermanager.service.INetptDetectionService;
import org.jeecg.modules.demo.sermanager.service.INetptDetectionRetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: netpt_detection
 * @Author: jeecg-boot
 * @Date:   2022-01-07
 * @Version: V1.0
 */
@Api(tags="检测管理")
@RestController
@RequestMapping("/sermanager/netptDetection")
@Slf4j
public class NetptDetectionController {
	@Autowired
	private INetptDetectionService netptDetectionService;
	@Autowired
	private INetptDetectionRetService netptDetectionRetService;
	
	/**
	 * 分页列表查询
	 *
	 * @param netptDetection
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "netpt_detection-分页列表查询")
	@ApiOperation(value="netpt_detection-分页列表查询", notes="netpt_detection-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(NetptDetection netptDetection,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) throws Exception {
		BeanUtils.populate(netptDetection,req.getParameterMap());
		QueryWrapper<NetptDetection> queryWrapper =new QueryWrapper();
		if(StringUtils.isNotBlank(netptDetection.getServe())){
			queryWrapper.inSql("id","select detection_id from netpt_policy where serve='"+netptDetection.getServe()+"'");
		}
		if(StringUtils.isNotBlank(netptDetection.getDetectionType())){
			queryWrapper.like("detection_type",netptDetection.getDetectionType());
		}
		Page<NetptDetection> page = new Page<NetptDetection>(pageNo, pageSize);
		IPage<NetptDetection> pageList = netptDetectionService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param netptDetectionPage
	 * @return
	 */
	@AutoLog(value = "netpt_detection-添加")
	@ApiOperation(value="netpt_detection-添加", notes="netpt_detection-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody NetptDetectionPage netptDetectionPage) throws Exception {
		NetptDetection netptDetection = new NetptDetection();
		PropertyUtils.copyProperties(netptDetection, netptDetectionPage);
		netptDetectionService.saveMain(netptDetection, netptDetectionPage.getNetptDetectionRetList());
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param netptDetectionPage
	 * @return
	 */
	@AutoLog(value = "netpt_detection-编辑")
	@ApiOperation(value="netpt_detection-编辑", notes="netpt_detection-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody NetptDetectionPage netptDetectionPage) throws Exception {
		NetptDetection netptDetection = new NetptDetection();
		PropertyUtils.copyProperties(netptDetection, netptDetectionPage);
		NetptDetection netptDetectionEntity = netptDetectionService.getById(netptDetection.getId());
		if(netptDetectionEntity==null) {
			return Result.error("未找到对应数据");
		}
		netptDetectionService.updateMain(netptDetection, netptDetectionPage.getNetptDetectionRetList());
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_detection-通过id删除")
	@ApiOperation(value="netpt_detection-通过id删除", notes="netpt_detection-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		netptDetectionService.delMain(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "netpt_detection-批量删除")
	@ApiOperation(value="netpt_detection-批量删除", notes="netpt_detection-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.netptDetectionService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_detection-通过id查询")
	@ApiOperation(value="netpt_detection-通过id查询", notes="netpt_detection-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) throws Exception {
		NetptDetection netptDetection = netptDetectionService.getById(id);
		List<NetptDetectionRet> netptDetectionRetList = netptDetectionRetService.selectByMainId(id);
		NetptDetectionPage entity=new NetptDetectionPage();
		PropertyUtils.copyProperties(entity,netptDetection);
		entity.setNetptDetectionRetList(netptDetectionRetList);
		if(netptDetection==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(entity);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_detection_ret通过主表ID查询")
	@ApiOperation(value="netpt_detection_ret主表ID查询", notes="netpt_detection_ret-通主表ID查询")
	@GetMapping(value = "/queryNetptDetectionRetByMainId")
	public Result<?> queryNetptDetectionRetListByMainId(@RequestParam(name="id",required=true) String id) {
		List<NetptDetectionRet> netptDetectionRetList = netptDetectionRetService.selectByMainId(id);
		return Result.OK(netptDetectionRetList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param netptDetection
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, NetptDetection netptDetection) throws Exception {
      // Step.1 组装查询条件查询数据
      QueryWrapper<NetptDetection> queryWrapper = QueryGenerator.initQueryWrapper(netptDetection, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //Step.2 获取导出数据
      List<NetptDetection> queryList = netptDetectionService.list(queryWrapper);
      // 过滤选中数据
      String selections = request.getParameter("selections");
      List<NetptDetection> netptDetectionList = new ArrayList<NetptDetection>();
      if(oConvertUtils.isEmpty(selections)) {
          netptDetectionList = queryList;
      }else {
          List<String> selectionList = Arrays.asList(selections.split(","));
          netptDetectionList = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
      }

      // Step.3 组装pageList
      List<NetptDetectionPage> pageList = new ArrayList<NetptDetectionPage>();
      for (NetptDetection main : netptDetectionList) {
          NetptDetectionPage vo = new NetptDetectionPage();
		  PropertyUtils.copyProperties(vo, main);
          List<NetptDetectionRet> netptDetectionRetList = netptDetectionRetService.selectByMainId(main.getId());
          vo.setNetptDetectionRetList(netptDetectionRetList);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "netpt_detection列表");
      mv.addObject(NormalExcelConstants.CLASS, NetptDetectionPage.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("netpt_detection数据", "导出人:"+sysUser.getRealname(), "netpt_detection"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }

}
